import { BaseWindow, ipcMain, screen } from 'electron'
import getConfig from '../config/sizeConfig'
import createLeftView from './views/leftView'
import createRightView from './views/rightView'
import icon from '../../resources/icon.png?asset'

const createWindow = () => {
  const config = getConfig();

  // 创建窗口
  const win = new BaseWindow({
    width: config.width,
    height: config.height,
    resizable: false,
    autoHideMenuBar: true,
    title: "AI审批助手",
    useContentSize: true,
    enableLargerThanScreen : true,
    ...(process.platform === 'linux' ? { icon } : {}),
  });

  const leftView = createLeftView(win, config);
  const rightView = createRightView(win, config);

  ipcMain.on('requestRequest', (event, responseText) => {
    console.log('XHR Response:', responseText);
  });

  ipcMain.on('ajax-request-completed', (event, responseText) => {
    console.log('XHR Response:', responseText);
  });

  ipcMain.on('run-process', (process) => {
    console.log('run process:', process);
  })

  // 监听右侧窗口发送的消息
  ipcMain.on('inject-button', () => {

    // 监听页面加载完成，注入脚本
    leftView.webContents.once('did-finish-load', () => {
      leftView.webContents.executeJavaScript(`
        setTimeout(function(){
          const btn = document.getElementById('btbtn');
          btn.removeAttribute('target')
          if(btn){
            btn.click()
          }else{
            new Error('btn-dl-or-clone not found')
          }
        }, 1000)  
      `);
    });

    leftView.webContents.loadURL('https://kimi.moonshot.cn/');

  });
};

export default createWindow;